"""
Case Type   : 功能测试
Case Name   : 双集群在主集群执行failover
Create At   : 2025/03/25
Owner       : @chen
Description :
    1.准备json配置
    2.构建双集群
gs_ddr -t start -m primary --json jsonfile -dc_type stream
gs_ddr -t start -m disaster_standby --json jsonfile -dc_type stream
    3.执行switchover
gs_ddr -t switchover
    4.执行switchover
gs_ddr -t switchover -m
    5.执行switchover
gs_ddr -t switchover -m xx
    6.恢复环境
Expect      :
    1.成功
    2.成功
    3.合理报错
    4.合理报错
    5.合理报错
    6.成功
History     :
"""

import unittest
import os
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.ComThread import ComThread
from testcase.SS_FUNC.gs_ddr.Dual_Cluster_Redution_utils import dcru
from yat.test import Node
logger = Logger()

class Dual_Cluster_Redution_Case0034(unittest.TestCase):
    def setUp(self):
        logger.info(f'--------{os.path.basename(__file__)} start--------')
        self.dcru = dcru()
        self.com = Common()
        self.p_pri_node = Node('P_PrimaryDbUser')
        self.s_pri_node = Node('S_PrimaryDbUser')
        self.tpcc_node = Node('S_Standby2DbUser')
        self.p_json_path = ''
        self.s_json_path = ''

    def test_shared_storage(self):
        text = '-----step1:准备json配置 expect:成功-----'
        logger.info(text)
        self.p_json_path, self.s_json_path = self.dcru.josn_generate()
        logger.info(f'主集群json_path:{self.p_json_path}')
        logger.info(f'备集群json_path:{self.s_json_path}')

        text = '-----step2:构建双集群 expect:成功-----'
        logger.info(text)
        logger.info(f'-----主集群下发命令-----')
        p_start = ComThread(self.dcru.gs_ddr_start, args=(self.p_pri_node,
            'primary', 
            f'--json {self.p_json_path}', '--disaster_type stream'))
        p_start.setDaemon(True)
        p_start.start()
        s_start = ComThread(self.dcru.gs_ddr_start, args=(self.s_pri_node,
            'disaster_standby', 
            f'--json {self.s_json_path}', '--disaster_type stream'))
        s_start.setDaemon(True)
        s_start.start()
        res = p_start.get_result()
        logger.info(res)
        self.assertIn('Successfully do dorado disaster recovery start', res)
        res = s_start.get_result()
        logger.info(res)
        self.assertIn('Successfully do dorado disaster recovery start', res)

        text = '-----step3:执行switchover expect:合理报错-----'
        logger.info(text)
        res = self.dcru.gs_ddr_switchover(self.p_pri_node)
        logger.info(res)
        self.assertIn('value[None] is invalid', res)

        text = '-----step4:执行switchover expect:合理报错-----'
        logger.info(text)
        res = self.dcru.gs_ddr_switchover(self.p_pri_node, '-m')
        logger.info(res)
        self.assertIn('-m option requires 1 argument', res)

        text = '-----step5:执行switchover expect:合理报错-----'
        logger.info(text)
        res = self.dcru.gs_ddr_switchover(self.p_pri_node, '-m xx')
        logger.info(res)
        self.assertIn('value[xx] is invalid', res)

    def tearDown(self):
        text = '-----step6:恢复环境 expect:成功-----'
        logger.info(text)
        logger.info(f'-----双集群解除灾备关系-----')
        res = self.dcru.gs_ddr_stop(self.p_pri_node, f'--json {self.p_json_path}')
        logger.info(res)
        self.assertIn('Successfully do dorado disaster recovery stop', res)
        logger.info(f'-----备集群恢复-----')
        res = self.dcru.gs_ddr_failover(self.s_pri_node)
        logger.info(res)
        self.assertIn('Successfully do dorado disaster recovery failover', res)
        self.p_pri_node.sh(f'rm -rf {self.p_json_path}').result()
        self.s_pri_node.sh(f'rm -rf {self.s_json_path}').result()
        logger.info(f'-----{os.path.basename(__file__)} end-----')
